<?php
/**
 * Created by PhpStorm.
 * User: u
 * Date: 2019/2/25
 * Time: 10:32
 */

namespace common\sources\read\operatorlog;


use common\models\craftsman\OperatorLog;

class OperatorLogAccess implements IOperatorLogAccess
{
    public function getList($data)
    {
        $where = " 1=1 ";
        if(!empty($data['operator_phone']))
        {
            $where .= " AND operator_phone = ".$data['operator_phone'];
        }
        return OperatorLog::find()
                ->select("id,username,create_time,login_ip,content,")
                ->where($where)
                ->orderBy("id desc")
                ->offset(($data['page_num'] -1)*$data['limit'])
                ->limit($data['limit'])
                ->asArray()
                ->all();
    }

    public function getCount($data)
    {
        $where = " 1=1 ";
        if(!empty($data['operator_phone']))
        {
            $where .= " AND operator_phone = ".$data['operator_phone'];
        }
        return OperatorLog::find()
            ->where($where)
            ->asArray()
            ->count();
    }

    public function GetExportLog($data)
    {
        $query = OperatorLog::find()
            ->select("id,username,create_time,login_ip,content,export,FROM_UNIXTIME(export_time) as export_time")
            ->where(['export' => 1]);
            if (!empty($data['search'])) {
                $search = $data['search'];
                $query = $query->where("username like :username or operator_phone like :operator_phone")
                    ->addParams([':username' => $search])
                    ->addParams([':operator_phone' => $search]);
            }
            if (!empty($data['begin_time'])) {
                $query = $query->andWhere([
                    '>',
                    'export_time',
                    $data['begin_time']
                ]);
            }
            if (!empty($data['end_time'])) {
                $query = $query->andWhere([
                    '<',
                    'export_time',
                    $data['end_time']
                ]);
            }
            $result = $query
                ->offset(($data['page_num'] -1)*$data['limit'])
                ->limit($data['limit'])
                ->asArray()
                ->all();
            return $result;
    }

    public function countExportLog($data) {
        $query = OperatorLog::find()
            ->select("id,username,create_time,login_ip,content,export,export_time")
            ->where(['export' => 1]);
        if (!empty($data['search'])) {
            $search = $data['search'];
            $query = $query->where("username like :username or operator_phone like :operator_phone")
                ->addParams([':username' => $search])
                ->addParams([':operator_phone' => $search]);
        }
        if (!empty($data['begin_time'])) {
            $query = $query->andWhere([
                '>',
                'export_time',
                $data['begin_time']
            ]);
        }
        if (!empty($data['end_time'])) {
            $query = $query->andWhere([
                '<',
                'export_time',
                $data['end_time']
            ]);
        }
        $result = $query
            ->asArray()
            ->count();
        return $result;
    }
}